<?php
use app\bootstrap\SetUp;
use app\core\auth\Identity;
use filsh\yii2\oauth2server\filters\auth\CompositeAuth;
use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter;
use filsh\yii2\oauth2server\Module;
use filsh\yii2\oauth2server\Request;
use filsh\yii2\oauth2server\Response;
use OAuth2\GrantType\RefreshToken;
use OAuth2\GrantType\UserCredentials;
use yii\filters\AccessControl;
use yii\filters\ContentNegotiator;
use yii\queue\file\Queue;
use yii\web\JsonResponseFormatter;
use zertex\avatar_generator\AvatarGenerator;

$params = array_merge(
    require __DIR__ . '/params.php',
    require __DIR__ . '/params_local.php'
);
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'language' => 'ru-RU',
    'basePath' => dirname(__DIR__),
    'bootstrap' => [
        'log',
        SetUp::class,
        [
            'class' => ContentNegotiator::class,
            'formats' => [
                'application/json' => 'json',
                'application/xml' => 'xml',
            ]
        ]
    ],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'modules' => [
        'oauth2' => [
            'class' => Module::class,
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => Identity::class,
            ],
            'components' => [
                'request' => function () {
                    return Request::createFromGlobals();
                },
                'response' => [
                    'class' => Response::class,
                ],
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => UserCredentials::class,
                ],
                'refresh_token' => [
                    'class' => RefreshToken::class,
                    'always_issue_new_refresh_token' => true
                ],
            ],
        ],
    ],
    'components' => [
        'response' => [
            'formatters' => [
                'json' => [
                    'class' => JsonResponseFormatter::class,
                    'prettyPrint' => YII_DEBUG,
                    'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
                ],
            ],
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'queue'         => [
            'class' => Queue::class,
            'path' => '@runtime/queue',
        ],
        'avatar'        => [
            'class'              => AvatarGenerator::class,
            'images_folder'      => '@webroot/static/avatars',
            'images_url'         => '@web/static/avatars',
            'size_width'         => 300,            // default: 300
            'font_size'          => 200,             // default: 200
            'salt'               => 'apx_im_salt',        // salt for image file names
            'texture'            => ['sun', 'rain'],   // texture name
            'texture_over_image' => false,
            'text_over_image'    => false,
        ],
        'user'               => [
            'identityClass' => Identity::class,
            'enableAutoLogin' => false,
            'enableSession' => false,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
//                ['class' => 'yii\rest\UrlRule', 'controller' => 'auth'],
//                ['class' => 'yii\rest\UrlRule', 'controller' => 'site'],

                'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',

                '' => 'site/index',
                'GET site/check-auth' => 'site/check-auth',

                'POST auth/login' => 'auth/login',
                'GET auth/user' => 'auth/user',

                'GET user/data' => 'user/data',

                'OPTIONS oauth2/<c:[\w-]+>' => '',
                'OPTIONS site/<c:[\w-]+>' => '',
                'OPTIONS auth/<c:[\w-]+>' => '',
                'OPTIONS user/<c:[\w-]+>' => '',
            ],
        ],
    ],
    'as authenticator' => [
        'class' => CompositeAuth::class,
        'except' => [
            'oauth2/rest/token',

            'site/index',

            'auth/login',
            'auth/user',
        ],
        'authMethods' => [
            ['class' => 'yii\filters\auth\HttpBearerAuth', 'except' => ['options']], //, 'except' => ['options']
            ['class' => 'yii\filters\auth\QueryParamAuth', 'tokenParam' => 'accessToken'],
        ]
    ],
    'as access' => [
        'class' => AccessControl::class,
        'except' => [
            'oauth2/rest/token',

            'site/index',

            'auth/login',
            'auth/user'
        ],
        'rules' => [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ],
    ],
    'as exceptionFilter' => [
        'class' => ErrorToExceptionFilter::class,
    ],
    'params' => $params,
];

return $config;
